Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Tinctures #844

Merged
merged 18 commits into from
Oct 22, 2023
Merged

Tinctures #844

merged 18 commits into from
Oct 22, 2023

Conversation

michaelboulton
Copy link
Member

@michaelboulton michaelboulton commented Feb 8, 2023

closes #114

Add utility for running things before/after stages, able to be specified at test or stage level. Example functions

def do_something_first(_):
    logger.info("starting stage!")


def time_request(_):
    t0 = time.time()
    yield
    t1 = time.time()
    logger.info("Request took %s", t1 - t0)


def print_response(_, extra_print="affa"):
    logger.info("STARTING:")
    (_, r) = yield
    logger.info("Response is %s (%s)", r, extra_print)

Used like

---
test_name: Test tincture

tinctures:
- function: tavern.helpers:time_request

stages:
- name: do something
  tinctures:
  - function: tavern.helpers:print_response
    extra_kwargs:
      extra_print: "blooble"
  - function: tavern.helpers:do_something_first
  request:
    url: "{host}/echo"
    method: POST
  response:
    status_code: 200

Each stage takes the stage dictionary as its first argument. If the tincture is a generator (if it defines a 'yield' like the second example), then the response will be returned to it (if you want it). That's all they can be used for - there is no context saved between stages. The syntax for defining the functions/extra args is the same as existing external functions.

Tinctures are called when the stage is run, so after any fixtures are evaluated.

As for the name - a tincture is

A tincture is typically an extract of plant or animal material dissolved in ethanol

I wanted a name that sounded sort of like 'fixture' and 'tavern'.

@michaelboulton michaelboulton mentioned this pull request Feb 8, 2023
@michaelboulton michaelboulton linked an issue Feb 8, 2023 that may be closed by this pull request
@michaelboulton michaelboulton self-assigned this Feb 8, 2023
@michaelboulton michaelboulton marked this pull request as ready for review August 5, 2023 18:24
@michaelboulton michaelboulton merged commit 74d70db into master Oct 22, 2023
7 checks passed
@michaelboulton michaelboulton deleted the 114-tincture-3.0 branch October 22, 2023 12:43
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Add support for before/after test stage actions
1 participant